package com.test.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.test.pojo.*;
import com.test.service.*;
import org.apache.velocity.shaded.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.lang.model.element.NestingKind;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;

/**
 * <p>
 * 商品类目 前端控制器
 * </p>
 *
 * @author yanglijun
 * @since 2020-12-12
 */
@RestController
@RequestMapping("/tb-category")
public class TbCategoryController {
    @Resource
    TbCategoryService categoryService;
    @Resource
    TbBrandService brandService;
    @Resource
    TbSpuService spuService;
    @Resource
    TbTemplateService tbTemplateService;
    @Resource
    TbSpecService specService;

    @RequestMapping("/updateImg")
    public boolean updateImg(@RequestBody MultipartFile myPic, HttpServletRequest request){
        //上传文件

        if (!myPic.isEmpty()) {
            //获取上传的目标路径
            String path = request.getSession().getServletContext().getRealPath("statics/images");
            //获取原文件名
            String filename = myPic.getOriginalFilename();
            //获取文件大小
            long size = myPic.getSize();
            //获取原文件后缀
            String suffix = FilenameUtils.getExtension(filename);
            //允许图片后缀名
            List<String> allowedSuffix = Arrays.asList("jpg", "png", "gif");
            if ((size <= 50000000) && (allowedSuffix.contains(suffix))) { //判断文件大小
                String newFileName = System.currentTimeMillis() + "." + suffix;
                File destFile = new File(path + File.separator + newFileName);
                try {
                    myPic.transferTo(destFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("++++++"+path + File.separator + newFileName);
                //http://localhost:8080/statics/images/1608866811644.jpg
                String imgPath="http://localhost:8080/statics/images/"+newFileName;
                TbSku sku = new TbSku();
                sku.setImage(newFileName);
            }
        }
        return  true;
    }
   @RequestMapping("/addGoodPart2")
    public boolean addGoodPart2(Integer spuId){
        //上传文件
      /*  if (!myPic.isEmpty()) {
            //获取上传的目标路径
            String path = request.getSession().getServletContext().getRealPath("statics/images/");
            //获取原文件名
            String filename = myPic.getOriginalFilename();
            //获取文件大小
            long size = myPic.getSize();
            //获取原文件后缀
            String suffix = FilenameUtils.getExtension(filename);
            //允许图片后缀名
            List<String> allowedSuffix = Arrays.asList("jpg", "png", "gif");
            if ((size<=5000000)&&(allowedSuffix.contains(suffix))) { //判断文件大小
                String newFileName =System.currentTimeMillis()+ "." +suffix;
                File destFile = new File(path + File.separator + newFileName);
                try {
                    myPic.transferTo(destFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("++++++"+newFileName);
                TbSku sku = new TbSku();
                sku.setImage(newFileName);*/
                /*user.setMyPicPath(newFileName);
                int i = userService.addUser(user);
                if (i>0) {
                    return "redirect:/frame";
                }*/


        return true;
    }



    @RequestMapping("/toAddGoodPart2")
    public Map toAddGoodPart2(Integer spuId){
        TbSpu spu= spuService.getById(spuId);
        // 规格参数组,商品规格
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",spu.getCategory3Id());
        List<TbCategory> categoryList= categoryService.list(queryWrapper);
        TbCategory c=categoryList.get(0);
        TbTemplate t= tbTemplateService.getById(c.getTemplateId());
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("template_id",c.getTemplateId());
        //list里的每一项的name 是前面的 尺码 等信息， options是后面的m，xl
        List<TbSpec> specList=specService.list(queryWrapper1);
        Map <String, Object> specMap=new HashMap<String, Object>();
        for ( TbSpec s:specList) {
        //TODO 把他 key是尺码 value是集合 s，m，l，xl，xxl
            String[] idArry = s.getOptions().split(",");
            specMap.put(s.getName(),idArry);
        }
        return specMap;
    }

    @RequestMapping("/addGoodPart1")
    public String addGoodPart1(String selectGoodsName,
                                String subtitle,
                                Integer selectCategory1,
                                Integer selectCategory2,
                                Integer selectCategory3,
                                Integer selectBrand,
                                String introduce,
                                String goodSN) {
        Integer spuId=new Random().nextInt(1000000000);
        String newSpuId=spuId.toString();
        TbSpu spu=new TbSpu(newSpuId,goodSN,selectGoodsName,
                subtitle,selectBrand,selectCategory1,selectCategory2
                ,selectCategory3,null,null,null,
                null,null,introduce,null,
                null,null,null,null,
                null,"0","0");
        spuService.save(spu);
        return newSpuId;
    }


        /**
         * 根据parentId来查询几级分类列表方法复用
         * 0为一级，
         * 对应的为二级
         * 然后是三级
         *
         * @param parentId
         * @return
         */
    @RequestMapping("/getCategoryByParentId")
    public List<TbCategory> getCategoryByParentId(String parentId) {


        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", parentId);
        return categoryService.list(queryWrapper);
    }

    @RequestMapping("/getBrandList")
    public List<TbBrand> getBrandList() {
        return brandService.list();
    }
}


